<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Enom extends MY_Controller
{
	public function __construct()
	{
        parent::__construct();
		if(! is_ajax())
		{
			redirect(BASE_URL);
		}
		$member = $this->session->userdata('user');
		$this->memberid=$member['userid'];
		
    }
	public function index()
	{
		echo "hello";
	}
	public function send_mail($to,$subject,$message)
	{
		$settingdata=$this->general_model->select_val("setting","*","id = 1");
		/*$config=Array(
			"protocol"=>"smtp",
			"smtp_host"=>"ssl://smtp.gmail.com",
			"smtp_port"=>465,
			"smtp_user"=>"niranjannsahoo@gmail.com",
			"smtp_pass"=>"paikarapursibu"
		);
		$this->load->library('email',$config);*/
		$this->load->library('email');
		$this->email->set_mailtype("html");
		$this->email->set_newline("\r\n");
		$this->email->from($settingdata['notification_email'],$settingdata['site_name']);
		$this->email->reply_to($settingdata['notification_email'], $settingdata['site_name']);	
		$this->email->to($to);		
		$this->email->subject($subject);		
		$this->email->message($message);
		$this->email->send();
	}
	
	public function check()
	{
		$url=$_POST['url'];
		$json['result']=enom($url);
		foreach($json['result']['api']['Domain'] as $domain)
		{
			$tld=substr($domain,strpos($domain, '.')+1); 
			$sld=substr($domain,0,strpos($domain, '.'));
			$price=$this->general_model->select_val("speedhost_tld_config","*","tld ='".$tld."'");
			$json['result']['api']['price'][]=$price['register_price'];
			$json['result']['api']['tld'][]=$tld;
			$json['result']['api']['sld'][]=$sld;
		}
		echo json_encode($json);
	}
	public function namespinner()
	{
		$url=$_POST['url'];
		$json['result']=enom($url);
		$i=0;
		foreach($json['result']['api']['namespin']['domains']['domain'] as $tldlist)
		{
			$domaincount=0;
			$domain_name=$tldlist['@name'];
			foreach($tldlist as $tld=>$value)
			{
				
				if($value=="y" && $domaincount==0)
				{
					$domain=substr($tld,strpos($tld, '@')+1);
					$domain_name=$domain_name.".".$domain;
					$price=$this->general_model->select_val("speedhost_tld_config","*","tld ='".$domain."'");
					$domaincount=1;
					$json['result']['api']['namespin']['domains']['domain'][$i]['price']=$price['register_price'];
					$json['result']['api']['namespin']['domains']['domain'][$i]['domain_name']=$domain_name;
				
				}
				
			}
			$i++;
		}
		echo json_encode($json);
	}
	public function premium()
	{
		$json['result']="The search returned no premium domains.";
		echo json_encode($json);
	}
	public function moredomain()
	{
		$url=$_POST['url'];
		$tldlist=array();
		$alltldlist=$this->general_model->select_rows("speedhost_tld_config","tld","id","ASC","10","0");
		foreach($alltldlist as $tld)
		{
			$tldlist[]=$tld['tld'];
		}
		$tldlist=implode(',',$tldlist);
		$url.="&tldlist=".$tldlist;
		$json['result']=enom($url);
		foreach($json['result']['api']['Domain'] as $tld)
		{
			$domain=substr($tld,strpos($tld, '.')+1); 
			$price=$this->general_model->select_val("speedhost_tld_config","*","tld ='".$domain."'");
			$json['result']['api']['price'][]=$price['register_price'];
			$json['result']['api']['tld'][]=$domain;
		}
		echo json_encode($json);
	}
	public function newdomain()
	{
		$url=$_POST['url'];
		$tldlist=array();
		$alltldlist=$this->general_model->select_rows("speedhost_tld_config","tld","id","ASC","10","0","new_tld = 1");
		foreach($alltldlist as $tld)
		{
			$tldlist[]=$tld['tld'];
		}
		$tldlist=implode(',',$tldlist);
		$url.="&tldlist=".$tldlist;
		$json['result']=enom($url);
		foreach($json['result']['api']['Domain'] as $tld)
		{
			$domain=substr($tld,strpos($tld, '.')+1); 
			$price=$this->general_model->select_val("speedhost_tld_config","*","tld ='".$domain."'");
			$json['result']['api']['price'][]=$price['register_price'];
			$json['result']['api']['tld'][]=$domain;
		}
		echo json_encode($json);
	}
	public function internationaldomain()
	{
		$url=$_POST['url'];
		$tldlist=array();
		$alltldlist=$this->general_model->select_rows("speedhost_tld_config","tld","id","ASC","10","0","international_tld = 1");
		foreach($alltldlist as $tld)
		{
			$tldlist[]=$tld['tld'];
		}
		$tldlist=implode(',',$tldlist);
		$url.="&tldlist=".$tldlist;
		$json['result']=enom($url);
		foreach($json['result']['api']['Domain'] as $tld)
		{
			$domain=substr($tld,strpos($tld, '.')+1); 
			$price=$this->general_model->select_val("speedhost_tld_config","*","tld ='".$domain."'");
			$json['result']['api']['price'][]=$price['register_price'];
			$json['result']['api']['tld'][]=$domain;
		}
		echo json_encode($json);
	}
	public function addtocart()
	{
		$sld = $this->input->post('sld'); // Assign posted product_id to $id
		$tld = $this->input->post('tld');
		$productid = $this->input->post('product_id');
		$ptype = $this->input->post('product_type');
		$qty = 1;
		$domaindetails=$this->general_model->select_val('speedhost_tld_config','*','tld ="'.$tld.'"');
		$pid=$domaindetails['tldid'];
		$regprice=$domaindetails['register_price'];
		$renewprice=$domaindetails['renew_price'];
		/*add to cart in enom*/
		$EndUserIP=$_SERVER['REMOTE_ADDR'];
		$url="&command=AddToCart&SLD=".$sld."&TLD=".$tld."&ProductType=".$ptype."&Quantity=1&EndUserIP=".$EndUserIP;
		//echo $url;
		$result=enom($url);
		//print_r($result);
		if(isset($result['error']))
		{
			$json['message']=$result['error'];
			
		}
		else
		{
			$cartdata = array(
				'prowid'	=>'',
				'id'		=> $productid.'_'.$sld.'.'.$tld,
				'itemid'	=> $result['api']['CartItemID'],
				'productid'	=> $productid,
				'ptype'   	=> $ptype,
				'pdesc'   	=> $ptype,
				'sld'   	=> $sld,
				'tld'   	=> $tld,
				'qty'     	=> $qty,
				'period'	=>1,
				'price'  => $regprice,
				'regprice'  => $regprice,
				'renewprice' => $renewprice,
				'name'   	=> $sld.'.'.$tld,
				'extraparam'=>array()
			);
			$this->cart->insert($cartdata);
			$json['message']="domain Successfully added to cart";
		}
		
		if($this->general_model->loggedin("user") == TRUE)
		{
			$storecart=$this->general_model->select_val('speedhost_member','cart','userid = '.$this->memberid);
			$cart=$this->cart->contents();
			if($storecart['cart']=='')
			{
				
				$updatecart=array("cart"=>serialize($cart));
				$this->general_model->update("speedhost_member",$updatecart,"userid = ".$this->memberid);
			}
			else
			{
				$pcart=unserialize($storecart['cart']);
				
				$newcart=array_merge($pcart,$cart);
				$this->cart->insert($newcart);
				$updatecart=array("cart"=>serialize($newcart));
				$this->general_model->update("speedhost_member",$updatecart,"userid = ".$this->memberid);
				
			}
			
		}
		
		echo json_encode($json);
	}
	public function updatecart()
	{
		$total = $this->cart->total_items();
		// Retrieve the posted information
		$item = $this->input->post('rowid');
		$itemid = $this->input->post('itemid');
	    $qty = $this->input->post('qty');
		$price= $this->input->post('price');
		$updatedata = array(
               'rowid' => $item,
               'qty'   => $qty,
			   'price' => $price,
        );
		/*update cart to cart in enom*/
		$EndUserIP=$_SERVER['REMOTE_ADDR'];
		$url="&command=updatecart&ItemID1=".$itemid."&ItemStatus1=on&ItemQty1=".$qty;
		//echo $url;
		$result=enom($url);
		if(isset($result['error']))
		{
			$json['message']=$result['error'];
			
		}
		else
		{
			$this->cart->update($updatedata);
		}
		// Update the cart with the new information
		
		
		$cart=$this->cart->contents();
		
		$data['cart']=$this->get_nested($cart);
		//print_r($carts);
		$carthtml=$this->load->view('front/cartcontent',$data,true);
		echo $carthtml;
	
	}
	public function removecart()
	{
		$total = $this->cart->total_items();
		// Retrieve the posted information
		$item = $this->input->post('rowid');
	    $qty = $this->input->post('qty');
		$itemid = $this->input->post('itemid');
		$cart=$this->cart->contents();
		foreach($cart as $items)
		{
			
			$url="&command=DeleteFromCart&ItemNumber=".$itemid;
			//echo $url;
			$result=enom($url);
			if(isset($result['error']))
			{
				$json['message']=$result['error'];
				
			}
			else
			{
				if($items['rowid']==$item || $items['prowid'] == $item)
				{
					$updatedata = array(
					   'rowid' => ($items['prowid']!='' ? $items['rowid'] : $item),
					   'qty'   => $qty,
					   'price' => $items['price'],
					);
					$this->cart->update($updatedata);
				}
			}
		}

		// Update the cart with the new information
		
		if($this->general_model->loggedin("user") == TRUE)
		{
			$storecart=$this->general_model->select_val('speedhost_member','cart','userid = '.$this->memberid);
			$cart=$this->cart->contents();	
			$updatecart=array("cart"=>serialize($cart));
			$this->general_model->update("speedhost_member",$updatecart,"userid = ".$this->memberid);
			
			
		}
		$cart=$this->cart->contents();
		
		$data['cart']=$this->get_nested($cart);
		//print_r($carts);
		$json['carthtml']=$this->load->view('front/cartcontent',$data,true);
		echo json_encode($json);
	
	}
	public function viewcart()
	{
		if($this->general_model->loggedin("user") == TRUE)
		{
			$storecart=$this->general_model->select_val('speedhost_member','cart','userid = '.$this->memberid);
			$cart=unserialize($storecart['cart']);
			
		}
		else
		{
			$cart=$this->cart->contents();
		}
		$carthtml='<div class="panel panel-danger">';
		$carthtml.=		'<div class="panel-heading">Items In Cart</div>';
		$carthtml.=		'<div class="cart_item">';
		$carthtml.=			'<table class="table" >';
		$carthtml.=				'<tbody>';
		foreach($cart as $item)
		{
			$carthtml.=				'<tr>';
			$carthtml.=					'<td>'.$item['name'].'</td>';
			$carthtml.=					'<td>'.$item['price'].'</td>';
			$carthtml.=				'</tr>';
		}
		$carthtml.=				'</tbody>';
		$carthtml.=			'</table>';
		$carthtml.=		'</div>';
		$carthtml.=		'<div class="panel-footer text-center">';
		$carthtml.=			'<a href="'.base_url('users/cart').'" class="btn btn-primary btn-xs">Checkout</a>';
		$carthtml.=		'</div>';
		$carthtml.=	'</div>';
		
		echo $carthtml;
	}
	public function addservice()
	{
		
		$prowid = $this->input->post('prowid');
		$parentid=explode(',',$prowid);
		$productid = $this->input->post('productid');
		
		$qty = 1;
		$carts=$this->cart->contents();
		//$carts=unserialize($cartdetails['cart']);
		$productdetails=$this->general_model->select_val('products','*','ptypeid ="'.$productid.'"');
		
		$price=$productdetails['price'];
		$ptype=$productdetails['product_type'];
		$name=$productdetails['product_name'];
		foreach($parentid as $pid)
		{
			$searcharray=$carts[$pid];
			$EndUserIP=$_SERVER['REMOTE_ADDR'];
			$url="&command=AddToCart&SLD=".$searcharray['sld']."&TLD=".$searcharray['tld']."&ProductType=".$ptype."&Quantity=1&EndUserIP=".$EndUserIP;
			//echo $url;
			$result=enom($url);
			//print_r($result);
			if(isset($result['error']))
			{
				$json['message']=$result['error'];
				
			}
			else
			{
				//echo $productid.'_'.$searcharray['sld'].'.'.$searcharray['tld'];
				$cartdata = array(
					'prowid'	=>$pid,
					'id'		=> $productid.'_'.$searcharray['sld'].'.'.$searcharray['tld'],
					'itemid'	=> $result['api']['CartItemID'],
					'productid'	=> $productid,
					'ptype'   	=> $ptype,
					'pdesc'   	=> $ptype,
					'sld'   	=> $searcharray['sld'],
					'tld'   	=> $searcharray['tld'],
					'qty'     	=> $qty,
					'period'	=>1,
					'price'   	=> $price,
					'regprice'  => '',
					'renewprice' => '',
					'name'   	=> $searcharray['sld'].'.'.$searcharray['tld'].' for '.$name,
					'extraparam'=>array()
				);
				
				//print_r($cartdata);
				$this->cart->insert($cartdata);
			}
		}
		
		if($this->general_model->loggedin("user") == TRUE)
		{
			$storecart=$this->general_model->select_val('speedhost_member','cart','userid = '.$this->memberid);
			$cart=$this->cart->contents();
			if($storecart['cart']=='')
			{
				
				$updatecart=array("cart"=>serialize($cart));
				$this->general_model->update("speedhost_member",$updatecart,"userid = ".$this->memberid);
			}
			else
			{
				$pcart=unserialize($storecart['cart']);
				
				$newcart=array_merge($pcart,$cart);
				$this->cart->insert($newcart);
				$updatecart=array("cart"=>serialize($newcart));
				$this->general_model->update("speedhost_member",$updatecart,"userid = ".$this->memberid);
				
			}
			
		}
		$cart=$this->cart->contents();
		$data['cart']=$this->get_nested($cart);
		//print_r($carts);
		$json['carthtml']=$this->load->view('front/cartcontent',$data,true);
		echo json_encode($json);
	}
	public function purchase($ptype)
	{
		$data['memberdetails']=$this->general_model->select_val("speedhost_member","*","userid = ".$this->memberid);
		
		$url="";
		$orderid="";
		if($ptype=="Register")
		{
			$url="&SLD=".$this->input->post('sld');
			$url.="&TLD=".$this->input->post('tld');
			$url.="&command=Purchase";
			$url.="&EndUserIP=".$_SERVER['REMOTE_ADDR'];
			$url.="&RegistrantOrganizationName=".$data['memberdetails']['organisation_name'];
			$url.="&RegistrantFirstName=".$data['memberdetails']['firstname'];
			$url.="&RegistrantLastName=".$data['memberdetails']['lastname'];
			$url.="&RegistrantJobTitle=".$data['memberdetails']['jobtitle'];
			$url.="&RegistrantAddress1=".$data['memberdetails']['address1'];
			$url.="&RegistrantAddress2=".$data['memberdetails']['address2'];
			$url.="&RegistrantCity=".$data['memberdetails']['city'];
			if($data['memberdetails']['rspchoice']=="S")
			$url.="&RegistrantStateProvince=".$data['memberdetails']['state'];
			else
			$url.="&RegistrantStateProvince=".$data['memberdetails']['province'];
			$url.="&RegistrantStateProvinceChoice=".$data['memberdetails']['rspchoice'];
			$url.="&RegistrantPostalCode=".$data['memberdetails']['zip'];
			$url.="&RegistrantCountry=".$data['memberdetails']['country'];
			$url.="&RegistrantEmailAddress=".$data['memberdetails']['account_email'];
			$url.="&RegistrantPhone=+".$data['memberdetails']['phone_code'].".".$data['memberdetails']['phone'];
			$url.="&RegistrantFax=+".$data['memberdetails']['fax_code'].".".$data['memberdetails']['fax'];
			$result=enom($url);
			$orderid=$result['api']['OrderID'];
		}
		elseif($ptype=="Renew")
		{
			$url="&SLD=".$this->input->post('sld');
			$url.="&TLD=".$this->input->post('tld');
			$url.="&command=Extend";
			$url.="&EndUserIP=".$_SERVER['REMOTE_ADDR'];
			$url.="&RegistrantOrganizationName=".$data['memberdetails']['organisation_name'];
			$url.="&RegistrantFirstName=".$data['memberdetails']['firstname'];
			$url.="&RegistrantLastName=".$data['memberdetails']['lastname'];
			$url.="&RegistrantJobTitle=".$data['memberdetails']['jobtitle'];
			$url.="&RegistrantAddress1=".$data['memberdetails']['address1'];
			$url.="&RegistrantAddress2=".$data['memberdetails']['address2'];
			$url.="&RegistrantCity=".$data['memberdetails']['city'];
			if($data['memberdetails']['rspchoice']=="S")
			$url.="&RegistrantStateProvince=".$data['memberdetails']['state'];
			else
			$url.="&RegistrantStateProvince=".$data['memberdetails']['province'];
			$url.="&RegistrantStateProvinceChoice=".$data['memberdetails']['rspchoice'];
			$url.="&RegistrantPostalCode=".$data['memberdetails']['zip'];
			$url.="&RegistrantCountry=".$data['memberdetails']['country'];
			$url.="&RegistrantEmailAddress=".$data['memberdetails']['account_email'];
			$url.="&RegistrantPhone=+".$data['memberdetails']['phone_code'].".".$data['memberdetails']['phone'];
			$url.="&RegistrantFax=+".$data['memberdetails']['fax_code'].".".$data['memberdetails']['fax'];
			$result=enom($url);
			$orderid=$result['api']['OrderID'];
		}
		elseif($ptype=="WebHosting")
		{
			
			$extraparam=json_decode($this->input->post('extraparam'));
			
			$url.="&command=CREATEHOSTACCOUNT";
			$url.="&EndUserIP=".$_SERVER['REMOTE_ADDR'];
			$url.="&hostaccount=".$extraparam->HostID;
			$url.="&HostPassword=".$extraparam->HostPW;
			$url.="&FullName=".$data['memberdetails']['firstname']." ".$data['memberdetails']['lastname'];
			$url.="&HostAccountEmail=".$data['memberdetails']['account_email'];
			
			$url.="&package=".$extraparam->Package;
			$url.="&OverageOption=".$extraparam->OverageOption;
			$result=enom($url);
			$orderid=$result['api']['OrderID'];
		}
		elseif($ptype=="Host")
		{
			
			//$extraparam=json_decode($this->input->post('extraparam'));
			/*purchase dns hosting*/
			$url="&SLD=".$this->input->post('sld');
			$url.="&TLD=".$this->input->post('tld');
			$url.="&numyears=".$this->input->post('period');
			$url.="&Renewname=1";
			$url.="&ChargeAmount=".$this->input->post('price');
			$url.="&command=purchasehosting";
			$url.="&EndUserIP=".$_SERVER['REMOTE_ADDR'];
			$url.="&RegistrantOrganizationName=".$data['memberdetails']['organisation_name'];
			$url.="&RegistrantFirstName=".$data['memberdetails']['firstname'];
			$url.="&RegistrantLastName=".$data['memberdetails']['lastname'];
			$url.="&RegistrantJobTitle=".$data['memberdetails']['jobtitle'];
			$url.="&RegistrantAddress1=".$data['memberdetails']['address1'];
			$url.="&RegistrantAddress2=".$data['memberdetails']['address2'];
			$url.="&RegistrantCity=".$data['memberdetails']['city'];
			if($data['memberdetails']['rspchoice']=="S")
			$url.="&RegistrantStateProvince=".$data['memberdetails']['state'];
			else
			$url.="&RegistrantStateProvince=".$data['memberdetails']['province'];
			$url.="&RegistrantStateProvinceChoice=".$data['memberdetails']['rspchoice'];
			$url.="&RegistrantPostalCode=".$data['memberdetails']['zip'];
			$url.="&RegistrantCountry=".$data['memberdetails']['country'];
			$url.="&RegistrantEmailAddress=".$data['memberdetails']['account_email'];
			$url.="&RegistrantPhone=+".$data['memberdetails']['phone_code'].".".$data['memberdetails']['phone'];
			$url.="&RegistrantFax=+".$data['memberdetails']['fax_code'].".".$data['memberdetails']['fax'];
			$result=enom($url);
			$orderid=$result['api']['OrderID'];
		}
		elseif($ptype=="POP")
		{
					
			//$extraparam=json_decode($this->input->post('extraparam'));
			/*purchase dns hosting*/
			$url="&SLD=".$this->input->post('sld');
			$url.="&TLD=".$this->input->post('tld');
			$url.="&Service=POP3";
			$url.="&Quantity=".$this->input->post('qty');
			$url.="&command=PurchaseServices";
			$result=enom($url);
			if(isset($result['error']))
			{
				$json['message']['error']=$result['error'];
				$json['htmlresult']	="";		
			}
			else
			{
				$url="&SLD=".$this->input->post('sld');
				$url.="&TLD=".$this->input->post('tld');
				
				$url.="&Quantity=".$this->input->post('qty');
				$url.="&command=PurchasePOPBundle";
				$result=enom($url);
				//print_r($result);
				$orderid=$result['api']['orderid'];
			}
			
			
		}
		elseif($ptype=="certificate")
		{
					
			//$extraparam=json_decode($this->input->post('extraparam'));
			/*purchase dns hosting*/
			$EndUserIP=$_SERVER['REMOTE_ADDR'];
			$url.="&EndUserIP=".$EndUserIP;
			$url.="&command=InsertNewOrder";
			$result=enom($url);
			$orderid=$result['api']['OrderID'];
			
		}
		//print_r($result);
		if(isset($result['error']))
		{
			$json['message']['error']=$result['error'];
			$json['htmlresult']	="";		
		}
		else
		{
			if($ptype=="Register")
			{			
				$purchasedata = array(
					'userid'	=>$this->memberid,
					'e_domain_id'=> $this->input->post('domainid'),
					'RegistrantPartyID'	=> $result['api']['RegistrantPartyID'],
					'sld'   	=> $this->input->post('sld'),
					'tld'   	=> $this->input->post('tld'),
					'exp_date'     	=> $result['api']['DomainInfo']['RegistryExpDate'],
					'order_date'	=>date("Y-m-d"),
					'dns'  => 0,
					'mail'  => 0,
					'webhosted' => 0,
					'status'   	=> 1,
					'pop'=>0,
					'idprotect'=>0,
					'auto_renew'=>0,
					'lastupdate'=>date("Y-m-d"),
					'e_order_id'=>$orderid,
				);
				$purchase_id=$this->general_model->insert('speedhost_domains',$purchasedata);
			
				$orderitem=array(
					"userid"=>$this->memberid,
					"e_order_id"=>$orderid,
					"sld"=>$this->input->post('sld'),
					"tld"=>$this->input->post('tld'),
					"productid"=>$this->input->post('productid'),
					"qty"=>$this->input->post('qty'),
					"price"=>$this->input->post('price'),
					"ptype"=>$this->input->post('ptype'),
					"expire_date"=>$result['api']['RegistryExpDate'],
					"status"=>1,
					"date_added"=>date("Y-m-d"),
				);
				$order_item_id=$this->general_model->insert('speedhost_order_item',$orderitem);
				$updatepurchase=array("expire_date"=>$result['api']['RegistryExpDate']);
				$this->general_model->update('speedhost_domain',$order_item_id,"sld = '".$this->input->post('sld')."' and tld = '".$this->input->post('tld')."'");
			}
			else if($ptype=="Renew")
			{
				$orderitem=array(
					"userid"=>$this->memberid,
					"e_order_id"=>$orderid,
					"sld"=>$this->input->post('sld'),
					"tld"=>$this->input->post('tld'),
					"productid"=>$this->input->post('productid'),
					"qty"=>$this->input->post('qty'),
					"price"=>$this->input->post('price'),
					"ptype"=>$this->input->post('ptype'),
					"expire_date"=>$result['api']['RegistryExpDate'],
					"status"=>1,
					"date_added"=>date("Y-m-d"),
				);
				$order_item_id=$this->general_model->insert('speedhost_order_item',$orderitem);
				$updatepurchase=array("expire_date"=>$result['api']['RegistryExpDate']);
				$this->general_model->update('speedhost_domain',$order_item_id,"sld = '".$this->input->post('sld')."' and tld = '".$this->input->post('tld')."'");
			
			}
			else if($ptype=="WebHosting")
			{
				$extraparam=json_decode($this->input->post('extraparam'));
				$packagedetails=$this->general_model->select_val("speedhost_hostingpackage","*","ostype = 'Linux' and packagename ='".$extraparam->Package."'");
				$orderitem=array(
					"userid"=>$this->memberid,
					"host_username"=>$extraparam->HostID,
					"host_password"=>$extraparam->HostPW,
					"full_name"=>$data['memberdetails']['firstname'].' '.$data['memberdetails']['lastname'],
					"email"=>$data['memberdetails']['account_email'],
					"package_name"=>$extraparam->Package,
					"product_id"=>11,
					"package_id"=>$packagedetails['package_id'],
					"db_type"=>$packagedetails['database_type'],
					"db_storage"=>$packagedetails['database_storage'],
					"pop_boxes"=>$packagedetails['popmailbox'],
					"disk_storage"=>$packagedetails['webstorage'],
					"bandwidth"=>$packagedetails['bandwidth'],
					
				
					"order_id"=>$orderid,
					"price"=>$result['api']['TotalPrice'],
					
					"IP"=>$result['api']['ServerIP'],
					"billing_cycle"=>$extraparam->period,
					"next_renew"=>date('Y-m-d', strtotime('+'.$extraparam->period .'months')),
					"status"=>1,
					"date_added"=>date("Y-m-d"),
				);
				$order_item_id=$this->general_model->insert('speedhost_webhosting',$orderitem);
				
				
			}
			else if($ptype=="Host")
			{
				$url.="&command=modifyns";
			
				$url.="&sld=".$this->input->post('sld');
				$url.="&tld=".$this->input->post('tld');
				$url.="&UseDNS=Default";
				
				$result1=enom($url);
				if(isset($result1['error']))
				{
					$json['message']['error']=$result1['error'];
					$json['htmlresult']	="";		
				}
				else{
				//$extraparam=json_decode($this->input->post('extraparam'));
					$orderitem=array(
						"userid"=>$this->memberid,
						"full_name"=>$data['memberdetails']['firstname'].' '.$data['memberdetails']['lastname'],
						"sld"=>$this->input->post('sld'),
						"tld"=>$this->input->post('tld'),
						"product_id"=>11,
						"order_id"=>$orderid,
						"price"=>$this->input->post('price'),
						"status"=>1,
						"date_added"=>date("Y-m-d"),
					);
					$order_item_id=$this->general_model->insert('speedhost_dnshosting',$orderitem);
				}
				
			}
			else if($ptype=="POP")
			{	
				//$extraparam=json_decode($this->input->post('extraparam'));
				$domaindetails=$this->general_model->select_val("speedhost_domains","*","sld = '".$this->input->post('sld')."' and tld ='".$this->input->post('tld')."' and userid =".$this->memberid);
				//print_r($result);
				$orderitem=array(
					"user_id"=>$this->memberid,
					"e_domain_id"=>$domaindetails['e_domain_id'],
					"sld"=>$this->input->post('sld'),
					"tld"=>$this->input->post('tld'),
					"prodid"=>38,
					"product_type"=>$this->input->post('ptype'),
					"order_date"=>date('Y-m-d'),
					"renew_date"=>date('Y-m-d', strtotime('+1 years')),
					"bundleid"=>json_encode($result['api']['Bundles']['BundleID']),
					"orderid"=>$orderid,
				);
				
				$order_item_id=$this->general_model->insert('speedhost_domain_addons',$orderitem);
				
			}
			elseif($ptype=="certificate")
			{
						
				//$domaindetails=$this->general_model->select_val("speedhost_domains","*","sld = '".$this->input->post('sld')."' and tld ='".$this->input->post('tld')."' and userid =".$this->memberid);
				//print_r($result);
				$extraparam=json_decode($this->input->post('extraparam'));
				$orderitem=array(
					"user_id"=>$this->memberid,
					"e_domain_id"=>'',
					"sld"=>'',
					"tld"=>'',
					"prodid"=>$this->input->post('productid'),
					"product_type"=>$extraparam->producttype,
					"order_date"=>date('Y-m-d'),
					"renew_date"=>date('Y-m-d', strtotime('+1 years')),
					"bundleid"=>'',
					"orderid"=>$orderid,
				);
				
				$order_item_id=$this->general_model->insert('speedhost_domain_addons',$orderitem);
				
				
			}
			$json['message']['success']="Successfully Created";
			$data['resultdata']=array(
				"orderid"=>$orderid
			);
			$json['htmlresult']=$this->load->view('front/purchasesuccess',$data,true);	
		}
		echo json_encode($json);
	}
	public function get_nested ($treelist)
	{
		
		$array = array();
		foreach ($treelist as $tree) {
			
			if (!$tree['prowid']) 
			{
				// This page has no parent
				$array[$tree['rowid']] = $tree;
			}
			else
			{
				// This is a child page
				$array[$tree['prowid']]['child'][]= $tree;
				//$this->get_nested($tree);
			}
		}
		return $array;
	}
	public function addwatchlist()
	{
		if($this->checklogin('users/watchlist'))
		{
			$tld = $this->input->post('tld');
			$listid = 0;
			
			$url="&command=TLD_Addwatchlist&DomainList=*.".$tld."&ListID=0";
			//echo $url;
			$result=enom($url);
			//print_r($result);
			if(isset($result['error']))
			{
				$json['message']=$result['error'];
				
			}
			else
			{
				if($result['api']['TLD_Watchlist']['Item']['Success']=="False")
				{
					$json['message']=$result['api']['TLD_Watchlist']['Item']['ResultMessage'];
				}
				else
				{
					$listdata = array(
						'watchlist_id'	=>$result['api']['TLD_Watchlist']['Item']['ID'],
						'domain_name'	=>$tld,
						'list_id'		=>$listid,
						'date_added'	=>date('Y-m-d')
					);	
			
					$this->general_model->insert("speedhost_watchlist",$listdata);
					$json['message']="Excellent! Your Watchlist has been updated";
				}
			}
		}
		else
		{
			$json['message']="Login Required";
			$json['login_url']="login";
			//$json['redirect_url']=$this->session->userdata('redirect');
		}
		echo json_encode($json);
	}
	public function domaintransfer()
	{
		$data['memberdetails']=$this->general_model->select_val("speedhost_member","*","userid = ".$this->memberid);
		$url="&command=TP_CreateOrder";
		$tld=substr($this->input->post('domainname'),strpos($this->input->post('domainname'), '.')+1);
		$sld=substr($this->input->post('domainname'),0,strpos($this->input->post('domainname'), '.'));
			
		$url.="&sld1=".$sld."&tld1=".$tld;
		$url.="&AuthInfo1=".$this->input->post('AuthInfo1');
		$url.="&orderType=".$this->input->post('orderType');
		$url.="&Lock=".$this->input->post('Lock');
		$url.="&Renew=".$this->input->post('Renew');
		if($this->input->post('access_password'))
		{
			$url.="&DomainPassword=".$this->input->post('DomainPassword');
		}
		$url.="&DomainCount=1";
		$url.="&UseContacts=".$this->input->post('UseContacts');
		$url.="&EndUserIP=".$_SERVER['REMOTE_ADDR'];
		if($this->input->post('UseContacts'))
		{
			$url.="&PreConfig=0";
		}
		else
		{
			$url.="&PreConfig=1";

		}
		$result=enom($url);
		//print_r($result);
		if(isset($result['error']))
		{
			$json['message']="$tld is an unsupported top level domain and can not be transfered";
		}
		else
		{
			$orderdetails=array(
				"transferorderid"=>$result['api']['transferorder']['transferorderid'],
				"orderdate"=>$result['api']['transferorder']['orderdate'],
				"ordertypeid"=>$result['api']['transferorder']['ordertypeid'],
				"ordertypedesc"=>$result['api']['transferorder']['ordertypedesc'],
				"statusdesc"=>$result['api']['transferorder']['statusdesc'],
				"authamount"=>$result['api']['transferorder']['authamount'],
				"transferorderdetailid"=>$result['api']['transferorder']['transferorderdetail']['transferorderdetailid'],
				"sld"=>$result['api']['transferorder']['transferorderdetail']['sld'],
				"tld"=>$result['api']['transferorder']['transferorderdetail']['tld'],
				"usecontacts"=>$result['api']['transferorder']['transferorderdetail']['usecontacts'],
				"status"=>1,
				"date_added"=>date('Y-m-d')
			);
			
			$this->general_model->insert("speedhost_domaintransfer",$orderdetails);
			$data['transferdetails']=array(
				"transferorderid"=>$result['api']['transferorder']['transferorderid'],
				"transferorderdetailid"=>$result['api']['transferorder']['transferorderdetail']['transferorderdetailid'],
				"sld"=>$result['api']['transferorder']['transferorderdetail']['sld'],
				"tld"=>$result['api']['transferorder']['transferorderdetail']['tld'],
				"lock"=>$this->input->post('Lock'),
				"renew"=>$this->input->post('Renew'),
				"contact"=>$result['api']['transferorder']['transferorderdetail']['usecontacts'],
				"password"=>'No',
				"price"=>$result['api']['transferorder']['transferorderdetail']['price'],
			);
			
			$data['countries']=$this->general_model->select("country");
			$data['us_states']=$this->general_model->select("zone","*","country_id = 223");
		
			$json['message']=$result['api']['transferorder']['transferorderdetail']['statusdesc'];
			if($this->input->post('UseContacts'))
			{
				$json['htmlresult']=$this->load->view('front/domaintransferorder',$data,true);
			}
			else
			{
				$json['htmlresult']=$this->load->view('front/domaintransfer_contact',$data,true);
			}
		}
		
		echo json_encode($json);
		
	}
	public function domaintransfer_contact()
	{
		//$data['memberdetails']=$this->general_model->select_val("speedhost_member","*","userid = ".$this->memberid);
		$url="&command=TP_UpdateOrderDetail";
			
		$url.="&TransferOrderDetailID=".$this->input->post('TransferOrderDetailID');
		$url.="&UseContacts=".$this->input->post('UseContacts');
		
		if(!$this->input->post('UseContacts'))
		{
			
			$url.="&RegistrantOrganizationName=".$this->input->post('RegistrantOrganizationName');
			$url.="&RegistrantFirstName=".$this->input->post('RegistrantFirstName');
			$url.="&RegistrantLastName=".$this->input->post('RegistrantLastName');
			$url.="&RegistrantJobTitle=".$this->input->post('RegistrantJobTitle');
			$url.="&RegistrantAddress1=".$this->input->post('RegistrantAddress1');
			$url.="&RegistrantAddress2=".$this->input->post('RegistrantAddress2');
			$url.="&RegistrantCity=".$this->input->post('RegistrantCity');
			
			$url.="&RegistrantStateProvince=".$this->input->post('RegistrantStateProvince');
			$url.="&RegistrantStateProvinceChoice=".$this->input->post('RegistrantStateProvinceChoice');
			$url.="&RegistrantPostalCode=".$this->input->post('RegistrantPostalCode');
			$url.="&RegistrantCountry=".$this->input->post('RegistrantCountry');
			$url.="&RegistrantEmailAddress=".$this->input->post('RegistrantEmailAddress');
			$url.="&RegistrantPhone=+".$this->input->post('RegistrantPhonecode').".".$this->input->post('RegistrantPhone');
			$url.="&RegistrantFax=+".$this->input->post('RegistrantFaxcode').".".$this->input->post('RegistrantFax');
			
		}
		$url.="&RegistrantUseContact=none";
		$url.="&TechUseContact=none";
		$url.="&AuxBillingUseContact=none";
		
		$result=enom($url);
		//print_r($result);
		if(isset($result['error']))
		{
			$json['message']=$result['error'];
		}
		else
		{
			$orderdetails=array(
				"statusdesc"=>$result['api']['transferorderdetail']['statusdesc'],
				"usecontacts"=>$result['api']['transferorderdetail']['usecontacts'],
				
			);
			
			$this->general_model->update("speedhost_domaintransfer",$orderdetails,"transferorderdetailid = '".$result['api']['transferorderdetail']['transferorderdetailid']."'");
			//$transferdetails=$this->general_model->select_val("speedhost_domaintransfer","*","transferorderdetailid = '".$result['api']['transferorderdetail']['transferorderdetailid']."'");
			$data['transferdetails']=array(
				"transferorderid"=>$this->input->post('transferorderid'),
				"transferorderdetailid"=>$result['api']['transferorderdetail']['transferorderdetailid'],
				"tld"=>$result['api']['transferorderdetail']['tld'],
				"sld"=>$result['api']['transferorderdetail']['sld'],
				"lock"=>$result['api']['transferorderdetail']['lock'],
				"renew"=>$result['api']['transferorderdetail']['renew'],
				"contact"=>$result['api']['transferorderdetail']['usecontacts'],
				"password"=>'No',
				"price"=>$result['api']['transferorderdetail']['price'],
			);
			
			$json['message']=$result['api']['transferorderdetail']['statusdesc'];
			
			$json['htmlresult']=$this->load->view('front/domaintransferorder',$data,true);
			
		}
		
		echo json_encode($json);
		
	}
	public function transfersubmit()
	{
		$url="&command=TP_SubmitOrder";	
		$url.="&TransferOrderID=".$this->input->post('TransferOrderID');
		$result=enom($url);
		if(isset($result['error']))
		{
			$json['message']=$result['error'];
		}
		else
		{
			$data['transferdata']=array(
				"orderid"=>$this->input->post('TransferOrderID')
			);
			$json['message']="Transfer order was submitted successfully";
			$json['htmlresult']=$this->load->view('front/domaintransfersuccess',$data,true);	
		}
		echo json_encode($json);
	}
	public function checklogin($url)
	{
		if($this->general_model->loggedin("user") == FALSE) 
		{
	  		$this->session->set_userdata("redirect",$url);
			//echo $this->session->userdata('redirect');			
			return false;
    	}
		else
		{
			return true;
		}
	}
	public function bulkdomain()
	{
		$productid = $this->input->post('ProductId');
		
		$domain=$this->input->post('domain');
		$product_type = $this->input->post('ProductType');
		$RegLock=$this->input->post('RegLock')?$this->input->post('AutoRenew'):'0';
		$AutoRenew=$this->input->post('AutoRenew')?$this->input->post('AutoRenew'):'0';
		//$domainArray = explode('\n', $domain);
		$domainArray=explode("\n", str_replace("\r", "", $domain));
		$error=0;
		$added=0;
		$successmessage='';
		//print_r($domainArray);
		if($domain=="")
		{
			$json['message']="Please Enter Domain Name in Proper Format";
		}
		
		elseif(count($domainArray))
		{
			$domainurl="";
			
			for($i=0;$i<count($domainArray);$i++)
			{
				//echo $domainArray[$i];
				if(!$this->setParseDomain($domainArray[$i]))
				{
					$successmessage.="Invalid domain name $domainArray[$i]"."<br />";
					//$json['message']="Invalid domain name $domainArray[$i]";
					$error=1;
					//break;
				}
				else
				{
					//echo $domainArray[$i];
					$TLD=substr($domainArray[$i],strpos($domainArray[$i], '.')+1); 
					$SLD=substr($domainArray[$i],0,strpos($domainArray[$i], '.'));
					$numyears=$this->input->post('numyears');
					$checkdomain=$this->general_model->count_row("speedhost_domains","sld = '".$SLD."' and tld = '".$SLD."' and userid =".$this->memberid);
					if(!$checkdomain && $product_type=="renew")
					{
						$successmessage.="$domainArray[$i] does not belong to account<br />";
						//$json['message']="$domainArray[$i] does not belong to account";
						//break;
						$error=1;
					}
				}
				$k=$i+1;
				$domainurl.="&sld$k=".$SLD."&tld$k=".$TLD."&numyears$k=".$numyears;
				
			}
			
			if(!$error)
			{
				$ListCount=count($domainArray);
				$url="&command=addbulkdomains";	
				$url.="&producttype=".$product_type;
				$url.="&listcount=".$ListCount;
				$url.=$domainurl;
				$url.="&AutoRenew=".$AutoRenew;
				$url.="&RegLock=".$RegLock;
				
				
				$result=enom($url);
				//print_r($result);
				if(isset($result['error']))
				{
					$json['message']=$result['error'];
				}
				else
				{
					if($result['api']['AddBulkDomains']['ListCount']==1)
					{
						$domainname=$result['api']['AddBulkDomains']['Item']['DomainName'];
						$TLD=substr($domainname,strpos($domainname, '.')+1); 
						$SLD=substr($domainname,0,strpos($domainname, '.'));
						$domaindetails=$this->general_model->select_val('speedhost_tld_config','*','tld ="'.$TLD.'"');
						$pid=$domaindetails['tldid'];
						$regprice=$domaindetails['register_price'];
						$renewprice=$domaindetails['renew_price'];
						if(isset($result['api']['AddBulkDomains']['Item']['ItemError']))
						{
							$json['message']=$result['api']['AddBulkDomains']['Item']['ItemError'];
						}
						else
						{
							$cartdata = array(
								'prowid'	=>'',
								'id'		=> $productid.'_'.$domainname,
								'itemid'	=> $result['api']['AddBulkDomains']['Item']['ItemId'],
								'productid'	=> $productid,
								'ptype'   	=> $product_type,
								'pdesc'   	=> $product_type,
								'sld'   	=> $SLD,
								'tld'   	=> $TLD,
								'qty'     	=> 1,
								'period'	=> $numyears,
								'price'  	=> $regprice,
								'regprice'  => $regprice,
								'renewprice'=> $renewprice,
								'name'   	=> $domainname,
								'extraparam'=>array()
							);
							$this->cart->insert($cartdata);
							$json['message']="domain Successfully added to cart";
						}
					}
					else
					{
						
						
						foreach($result['api']['AddBulkDomains']['Item'] as $item)
						{
							$domainname=$item['DomainName'];
							$TLD=substr($domainname,strpos($domainname, '.')+1); 
							$SLD=substr($domainname,0,strpos($domainname, '.'));
							$domaindetails=$this->general_model->select_val('speedhost_tld_config','*','tld ="'.$TLD.'"');
							$pid=$domaindetails['tldid'];
							$regprice=$domaindetails['register_price'];
							$renewprice=$domaindetails['renew_price'];
							if(isset($item['ItemError']))
							{
								$successmessage.=$item['ItemError'].'<br />';
								//$json['message']=$errormessage;
								//$error=1;
								//break;
							}
							else
							{
								$cartdata = array(
									'prowid'	=>'',
									'id'		=> $productid.'_'.$domainname,
									'itemid'	=> $item['ItemId'],
									'productid'	=> $productid,
									'ptype'   	=> $product_type,
									'pdesc'   	=> $product_type,
									'sld'   	=> $SLD,
									'tld'   	=> $TLD,
									'qty'     	=> 1,
									'period'	=> $numyears,
									'price'  	=> $regprice,
									'regprice'  => $regprice,
									'renewprice'=> $renewprice,
									'name'   	=> $domainname,
									'extraparam'=>array()
								);
								$this->cart->insert($cartdata);
								$successmessage.="$domainname Successfully added to cart<br/>";
							}							
						}
						$json['message']=$successmessage;
					}
				}
			}
			else
			{
				$json['message']=$successmessage;
			}
		} 
		
		echo json_encode($json);
	}
	function setParseDomain($domainName)
	{
		$parts=array();
		if ( !preg_match('/^([a-z0-9]+[a-z0-9\-]*[a-z0-9]+)\.([a-z]+[a-z\.]*[a-z]+)$/i', $domainName, $parts) || !$this->isValidSLD($parts[1]) )
		{
		  return false;
		}
		else
		{
			return true;
		}
	}
	function isValidSLD($sld)
	{
		if( preg_match('/^[a-z0-9]+[a-z0-9\-]*[a-z0-9]+$/i', $sld) && strlen($sld) < 64 && substr($sld, 2, 2) != '--' )
		{
		  return true;
		}
		else
		{
		  return false;
		}
	}
	
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */